package top.aylan.org.ctrl.admin;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.aylan.org.entity.Project;
import top.aylan.org.service.CategoryService;
import top.aylan.org.service.ProjectService;
import top.aylan.org.tools.Directory;
import top.aylan.org.tools.FileUploader;
import top.aylan.org.tools.Pager;
import top.aylan.org.tools.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2018/12/20.
 */
@Controller
@RequestMapping(value = "/admin/art")
public class AdminArtController {

    @Resource
    CategoryService categoryService;

    @Resource
    ProjectService projectService;

    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap, @RequestParam(value = "page",defaultValue = "1") int page){

        Pager pager = new Pager();
        pager.setCurrentPage(page);

        try {
            pager.setList(projectService.selectProjectByPage(pager));
            modelMap.addAttribute("pager",pager);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "admin/articals";
    }

    @RequestMapping("/add")
    public String add(ModelMap modelMap){

        try {
           modelMap.addAttribute("categories",categoryService.selectAllCategory());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/art-add";
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam(defaultValue = "0",value = "id")int id,ModelMap modelMap){

        try {
            modelMap.addAttribute("data",projectService.findById(id));
            modelMap.addAttribute("categories",categoryService.selectAllCategory());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/art-edit";
    }

    @RequestMapping(value = "/save")
    public String save(@RequestParam(value = "title",defaultValue = "title")String title,
                       @RequestParam(value="categoryId",defaultValue = "0")Integer categoryId,
                       @RequestParam(value = "introduction",defaultValue = "")String introduction,
                       @RequestParam(value = "id",defaultValue = "0") Integer id,
                       @RequestParam(value = "content",defaultValue = "") String content,
                       MultipartFile file,
                       HttpServletRequest request
    ){

        byte status = 0;
        if(StringUtils.isEmpty(request.getParameter("status")) && "on".equals(request.getParameter("status"))){
            status = 1;
        }
        Project project = new Project();
        project.setType((byte)1);
        project.setTitle(title);
        project.setContent(content);
        project.setCategoryId(categoryId);
        project.setStatus(status);
        project.setIntroduction(introduction);

        FileUploader fileUploader = new FileUploader();
        Result res = fileUploader.upload(file, Directory.ART);
        if(res.getCode() == 1){
            project.setThumb(res.getData().get("path").toString());
        }
        try {
            if(id == 0){
                project.setCreateTime(new Date());
                projectService.save(project);
            }else{
                projectService.updateById(id,project);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/admin/art/index";
    }

    /*
     *
     */
    @RequestMapping(value = "/state",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject state(@RequestParam(value = "id",defaultValue = "0")int id,
                            @RequestParam(value = "state",defaultValue = "0") byte status
    ){
        JSONObject object = new JSONObject();
        Project project = new Project();
        project.setId(id);
        project.setStatus(status);
        try {
            projectService.updateById(id,project);
            object.put("code",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id",defaultValue = "0") int id){

        try {
            projectService.delete(id);
            return "redirect:/admin/art/index";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin/articals";
    }
}
